package com.cai.commons.utils;

import sun.misc.BASE64Encoder;

import java.security.MessageDigest;

/**
 * @Author CWX
 * @Date 2017/7/22 22:55
 * MD5化
 */
public class SecurityMD5 {
    public static String MD5(String code){
        try{
            if (code.trim()==null){
                //校验数据是否为空
                return null;
            }
        MessageDigest md5=MessageDigest.getInstance("MD5");
        BASE64Encoder base64Encoder=new BASE64Encoder();
        String result=base64Encoder.encode(md5.digest(code.getBytes("utf-8")));
        return result;
        }catch (Exception e){
            System.out.println("md5化失败");
            return  "fail";
        }
    }
    public  static String passwordToMD5(String password){
        return MD5(password);
    }
    public  static boolean  pwdToBasePwdMd5(String pwd ,String basepwd){
        if (pwd.trim()==null && basepwd.trim()==null){ //校验传入数据值是否为空
            return false;
        }
        String password=MD5(pwd);
        if(pwd==basepwd){ //俩值相等
            return true;
        }else{ //俩值不相等
            return false;
        }
    }
}
